const DB = require('../../utils/db');

module.exports = {
	list: async (keywords) => {
		let aql = `select id, name, remark,date_format(create_time,'%Y-%m-%d %H:%i:%s') createTime, 
                       date_format(update_time,'%Y-%m-%d %H:%i:%s') updateTime  from t_label where name like '%${keywords}%'`;
		let [list] = await DB.query(aql);
		return list;
	},
	add: async (name, remark) => {
		let [list] = await DB.query('select name from t_label where name=?', [name]);
		if (list.length) throw new Error('标签已存在');
		await DB.query('insert into t_label (name, remark) values(?,?)', [name, remark]);
	},
	edit: async (name, remark, id) => {
		await DB.query(`update t_label set name=?, remark=? where id =?`, [name, remark, id]);
	},
	del: async (id) => {
		await DB.query('delete from t_label where id=?', [id]);
	},
	detail: async (id) => {
		let [list] = await DB.query(
			`select id, name, remark,date_format(create_time,'%Y-%m-%d %H:%i:%s') createTime, 
                  date_format(update_time,'%Y-%m-%d %H:%i:%s') updateTime from t_label where id=?`,
			[id]
		);
		if (!list.length) throw new Error('标签ID有误');

            return list[0];
	}
};
